package com.woniu.woniuticket.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.woniu.woniuticket.common.Result;
import com.woniu.woniuticket.entity.*;
import com.woniu.woniuticket.service.IOrderService;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author spirit
 * @since 2020-01-02
 */

@RestController
@Slf4j
@RequestMapping("/woniuticket/order")
@CrossOrigin("*")
public class OrderController {


    @Autowired
    private IOrderService ios;

    //对前端的时间类型参数进行转换
    @InitBinder
    public void init(WebDataBinder binder){
        binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
    }

    //查询所有订单
    @RequestMapping("getorder")
    public Result getOrder(Integer studentId){
        System.out.println("学生id："+studentId);
        List<OrderAllInfo> order = ios.getOrder(studentId);
        return Result.success(order);
    }

    //取消订单
    @RequestMapping("delorder")
    public void delorder(Integer orderId){
        System.out.println(orderId);
        ios.delOrder(orderId);
    }

    //退款
    @RequestMapping("returnmoney")
    public Result remoney(Integer orderId){
        System.out.println(orderId);
        ios.remoney(orderId);
        return Result.success();
    }

    //二维码接收
    @RequestMapping("qrorder")
    public Result qrorder(Integer orderId)throws Exception{
        System.out.println("二维码："+orderId);
        int result = ios.qrorder(orderId);
        System.out.println("二维码结果："+result);
        return Result.success(result);
    }

    //持续接收来自app端的请求
    @RequestMapping("continuedorder")
    public Result continuedorder(Integer orderId){
        int result = ios.selectOrder(orderId);
        System.out.println(result);
        return Result.success(result);
    }

    //生成订单，同时对座位进行更新在seatbuy表
    @RequestMapping("productorder")
    @ResponseBody
    public Result productorder(Integer studentId, BigDecimal orderPrice,String orderName,String seat, Integer showSessionId, String selectSeatCount,Integer ticketNumber){
        System.out.println();
        System.out.println(seat+"~~~~"+showSessionId+"~~~~"+selectSeatCount);
        //把json转成java
        Gson gson = new Gson();
        Type selectSeatListType = new TypeToken<ArrayList<SelectSeat>>() {}.getType();
        List<SelectSeat> selectSeats = gson.fromJson(selectSeatCount, selectSeatListType);
        OrderForm result = ios.productOrder(studentId,orderPrice,orderName,selectSeats,seat,showSessionId,ticketNumber);
        log.info("订单是否生成成功(1还是0）:"+result);
        Result result1 = new Result();
        result1.setData(result);
        if(result==null){
            result1.setCode("0");
            return result1;
        }
        result1.setCode("1");
        return result1;
    }
    //查询指定订单状态
    @RequestMapping("findorderstatus")
    public Result findOrderStatus(Integer orderId){
        int res = ios.selectOrder(orderId);
        System.out.println("倒计时的参数："+res);
        return Result.success(res);
    }
    //查询指定订单
    @RequestMapping("selectsomeorder")
    public int selectSomeOrder(Integer orderId){
        System.out.println("指定订单："+orderId);
        int res = ios.findSomeOrder(orderId);
        System.out.println(res);
        return res;
    }

    //查询订单时间
    @RequestMapping("findordertime")
    public Result findOrderTime(Integer orderId){
        String of=ios.findOrderTime(orderId);
        System.out.println("查询指定订单信息："+of);
        return Result.success(of);
    }

    @RequestMapping("selectOrderByStatus")
    public Result selectOrderByStatus(@RequestBody Form form){
        Page<OrderForm> ordersPage = new Page<>(form.getPageIndex(),10,true);
        IPage<OrderForm> orders = ios.selectOrderByStatus(ordersPage, form);
        List<OrderForm> records = orders.getRecords();
        long total = orders.getTotal();
        log.info("总的数据条数为:"+total);
        log.info("具体的数据为:"+records);
        Map<String,Object> map = new HashMap<>();
        map.put("pageTotal",total);
        map.put("tableData",records);
        return Result.success(map);
    }
}

